package com.thinkfit.jy.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.common.lib.activity.BaseActivity;
import com.common.lib.fragment.BaseFragment;
import com.common.lib.manager.DataManager;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.thinkfit.jy.R;
import com.thinkfit.jy.activity.MainADActivity;
import com.thinkfit.jy.activity.MainActivity;
import com.thinkfit.jy.activity.MainBMWActivity;
import com.thinkfit.jy.activity.MainXMActivity;
import com.thinkfit.jy.utils.BackButtonViewUtils;
import com.thinkfit.jy.utils.CountDownViewUtils;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.thinkfit.jy.utils.DialogCloseApkViewUtils;
import com.thinkfit.jy.utils.DialogViewUtils;
import com.thinkfit.jy.utils.LoadViewUtils;
import com.thinkfit.jy.utils.SafetyLockViewUtils;
import com.thinkfit.jy.utils.SetTargetViewUtils;
import com.thinkfit.jy.utils.ShowBackButtonViewUtils;
import com.thinkfit.jy.utils.ShowSpeedViewUtils;
import com.thinkfit.jy.utils.SportsStatusBarViewUtils;

import me.jessyan.retrofiturlmanager.RetrofitUrlManager;

/**
 * 地区选择界面
 */
public class SwitchRegionFragment extends BaseFragment<EmptyContract.Presenter> implements EmptyContract.View {

    private String BaseUrl;


    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }

    @Override
    protected int getLayoutId() {
//        return DataManager.Companion.getInstance().getMainStyle() == 3 ? R.layout.fragment_switch_region_xm : R.layout.fragment_switch_region;
        return R.layout.fragment_region;
    }

    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        setViewsOnClickListener(R.id.llAsia, R.id.llAmerica, R.id.llEurope);
        showRegion();
    }

    private void showRegion() {
        int region = DataManager.Companion.getInstance().getRegion();
        if (region == 0) {
            setViewVisible(R.id.ivAsia);
            setViewGone(R.id.ivAmerica, R.id.ivEurope);
        } else if (region == 1) {
            setViewVisible(R.id.ivAmerica);
            setViewGone(R.id.ivAsia, R.id.ivEurope);
        } else {
            setViewVisible(R.id.ivEurope);
            setViewGone(R.id.ivAsia, R.id.ivAmerica);
        }
    }



    @Override
    public void onStop() {
        super.onStop();
        System.out.println("ddddddddddddddddddddddd   SwitchRegionFragment ");
        DeviceTypeUtils.showBackButton(getContext().getApplicationContext());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvBack:
                getParentFragmentManager().popBackStack();
                break;
            case R.id.llAsia:
                DataManager.Companion.getInstance().saveRegion(0);
                DataManager.Companion.getInstance().saveLanguage(0);
                showRegion();
                switchRegion();
                break;
            case R.id.llAmerica:
                DataManager.Companion.getInstance().saveRegion(1);
                DataManager.Companion.getInstance().saveLanguage(1);
                showRegion();
                switchRegion();
                break;
            case R.id.llEurope:
                DataManager.Companion.getInstance().saveRegion(2);
                DataManager.Companion.getInstance().saveLanguage(1);
                showRegion();
                switchRegion();
                break;
        }
    }

    private void switchRegion() {
        ((BaseActivity) getActivity()).showProgressDialog();
        if (DataManager.Companion.getInstance().getRegion() == 0) {
            BaseUrl="https://www.thinkfiter.com";
        } else {
            BaseUrl= "https://en.thinkfiter.com";
        }

        RetrofitUrlManager.getInstance().setGlobalDomain(BaseUrl);
        getView().postDelayed(new Runnable() {
            @Override
            public void run() {
                SportsStatusBarViewUtils.getInstance().invaliView();
                ShowSpeedViewUtils.getInstance().invaliView();
                DialogViewUtils.getInstance().invaliView();
                BackButtonViewUtils.getInstance().invaliView();
                SetTargetViewUtils.getInstance().invaliView();
                DialogCloseApkViewUtils.getInstance().invaliView();
                LoadViewUtils.getInstance().invaliView();
                ShowBackButtonViewUtils.getInstance().cleanView();
                CountDownViewUtils.getInstance().invaliView();
                SafetyLockViewUtils.getInstance().invaliView();
                dismissProgressDialog();
                ((BaseActivity) getActivity()).finishAllActivity();
                openActivity(MainActivity.class);
//                switch (DataManager.Companion.getInstance().getMainStyle()){
//                    case 0:
//                        openActivity(MainActivity.class);
//                        break;
//                    case 1:
//                        openActivity(MainADActivity.class);
//                        break;
//                    case 2:
//                        openActivity(MainBMWActivity.class);
//                        break;
//                    case 3:
//                        openActivity(MainXMActivity.class);
//                        break;
//                }
//                SystemUtils.getInstance().restartApp(getContext());

            }
        }, 2000);
    }
}
